//1171. 从链表中删去总和值为零的连续节点


#include <stdlib.h>
 struct ListNode {
    int val;
    struct ListNode *next;
 };
struct ListNode* removeZeroSumSublists(struct ListNode* head){
    struct ListNode* newhead = (struct ListNode*)malloc(sizeof(struct ListNode));
    if(NULL == newhead)
    {
        perror("malloc fail:");
        return NULL;
    }
    newhead->next = head;

    struct ListNode* p = newhead;
    struct ListNode* q = p->next;
    int x = 0;
    while(p)
    {
        x = 0;
        q = p->next;
        while(q)
        {
            x -= q->val;
            if(0 == x)
            {
                p->next = q->next;
            }
            q = q->next;
        }
        p = p->next;
    }
    head = newhead->next;
    free(newhead);
    return head;
}